<?php

use backend\components\helper\Menu;
use backend\components\helper\EasyHtml;
use nickdenry\grid\toggle\assets\RoundSwitchAsset;
use nickdenry\grid\toggle\assets\RoundSwitchThemeAsset;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */
/* @var $searchModel mdm\admin\models\searchs\Menu */

$this->title = Yii::t('rbac-admin', 'Menus');
$this->params['breadcrumbs'][] = $this->title;

RoundSwitchAsset::register($this);
RoundSwitchThemeAsset::register($this);

?>
<style>
    #menu-tree thead tr th{
        width: 17%;
    }
    #menu-tree .treegrid-expander {
        height: 13px;
    }
</style>
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header with-border">
                <h3 class="box-title"><?= EasyHtml::encode($this->title) ?></h3>
                <div class="box-tools pull-right">
                    <?= EasyHtml::iconLink('新增', ['create'], [
                        'icon' => 'plus',
                        'class' => 'btn btn-success'
                    ]) ?>
                </div>
            </div>
            <div class="box-tools form-inline">
                <?php //echo $this->render('_search', ['model' => $searchModel]); ?>
                <div class="box-body">
                    <?= \jianyan\treegrid\TreeGrid::widget([
                        'id' => 'menu-tree',
                        'options' => [
                            'class' => 'table table-striped table-bordered table-hover table-list'
                        ],
                        'beforeRow' => function($model, $key, $index, $treeGrid){
                            $model->data = json_decode($model->data,true)??[];
                        },
                        'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => ''],
                        'dataProvider' => $dataProvider,
                        'keyColumnName' => 'id',
                        'parentColumnName' => 'parent',
                        'parentRootValue' => null, //first parentId value
                        'pluginOptions' => [
                            'initialState' => 'expanded',//expanded 所有节点都展开,collapsed 所有节点都折叠
                        ],
                        'columns' => [
                            //'id',
                            [
                                'label' => '名称',
                                'format' => 'raw',
                                'attribute' => 'data',
                                'value' => function($model){
                                    return '&nbsp&nbsp <i class="text-info fa fa-'.($model->data['icon']??Menu::$defaultIcon).'"></i> &nbsp&nbsp'.$model->name;
                                },
                            ],
                            //'parent',
                            'route',
                            [
                                'class' => \yii2mod\editable\EditableColumn::class,
                                'attribute' => 'order',
                                'url' => ['update-order'],
                                'clientOptions' => [
                                    'emptytext'=>'0',
                                    'success' => new \yii\web\JsExpression('function(res){if(!res.code){layer.msg(res.message);return false;}}')
                                ]
                            ],
                            [
                                'label' => '显示',
                                'format' => 'raw',
                                'class' => \backend\components\widget\toggle\RoundSwitchColumn::class,
                                'attribute' => 'visible',
                                'headerOptions' => [
                                    'width'=>'150',
                                ],
                                'value' => function($model){
                                    return $model->data['visible']??1;
                                },
                            ],
                            [
                                'class' => 'backend\components\widget\adminLTE\ActionColumn',
                                'template' => '{update} {delete}',
                            ],
                        ]
                    ]); ?>
                </div>
            </div>
        </div>
    </div>
</div>
